# -*- coding:UTF-8 -*-

# ！user/bin/env python3
# -*-coding: utf-8 -*-
__author__ = ''

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from PIL import Image
import random
# from info import username, password

username = ''
password = ''
def login(user, passwd):
    username = wait.until(EC.presence_of_element_located((By.ID, 'login-username')))
    username.send_keys(user)

    password = wait.until(EC.presence_of_element_located((By.ID, 'login-passwd')))
    password.send_keys(passwd)
    time.sleep(1)

    button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'btn-login')))
    button.click()


def get_image():
    geetest_canvas_bg = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_bg')))
    geetest_canvas_bg.screenshot('bg.png')
    image_bg = Image.open('bg.png')

    js = 'var change = document.getElementsByClassName("geetest_canvas_fullbg");change[0].style = "display: block;"'
    driver.execute_script(js)
    time.sleep(2)
    geetest_canvas_fullbg = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_fullbg')))
    geetest_canvas_fullbg.screenshot('fullbg.png')
    image_fullbg = Image.open('fullbg.png')

    return image_bg, image_fullbg


def get_diff_location(image1, image2):
    '''

    通过像素对比 找到缺口位置
    :param image1:
    :param image2:
    :return:
    '''
    for x in range(60, image1.size[0]):
        for y in range(1, image2.size[1]):
            if is_similar(image1, image2, x, y) == False:
                # 判断成立 表示xy这个点 两张图的像素点是不一样的
                return x


def is_similar(image1, image2, x, y):
    pixel1 = image1.getpixel((x, y))
    pixel2 = image2.getpixel((x, y))  # 元组

    for i in range(0, 3):
        if abs(pixel1[i] - pixel2[i]) >= 50:
            return False

    return True


def get_track(x):
    '''
    模拟匀加速运动
    :param x:
    :return:
    '''
    v = 0
    t = 0.2
    tracks = []
    current = 0
    # 到达mid值之和开始减速
    mid = x * 5 / 8
    x = x + 10
    while current < x:
        if current < mid:
            a = random.randint(1, 3)
        else:
            a = -random.randint(2, 4)
        # a = 2
        v0 = v
        s = v0 * t + 0.5 * a * (t ** 2)
        current += s
        tracks.append(round(s))
        v = v0 + a * t
    for i in range(4):
        tracks.append(-random.randint(1, 4))
    return tracks


def main(driver, elem):
    image_bg, image_fullbg = get_image()
    x = get_diff_location(image_bg, image_fullbg)
    print(x)

    tracks = get_track(x - 7)
    ActionChains(driver).click_and_hold(elem).perform()
    for x in tracks:
        ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
    ActionChains(driver).release(elem).perform()
    time.sleep(1)


if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.maximize_window()  # 截图时的问题 若报错 删掉这句话
    driver.get('https://passport.bilibili.com/login')

    wait = WebDriverWait(driver, 10)

    login(username, password)

    elem = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_slider_button')))

    main(driver, elem)


